package _05thread;

import java.io.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @description:
 * @author: songtao@cskaoyan.onaliyun.com
 **/

/*
用Callable的方式去实现之前的多线程文件复制
复制成功后,返回复制成功的文件路径,并利用Callable的方式接收返回值,
然后把这几个路径都保存到新的文件中.
 */
public class Work9 {
    public static void main(String[] args) {
        // 创建FutureTask
        FutureTask<String> futureTask =
                new FutureTask<>(new CopyFile2("D:\\temp\\aa.jpg", "aa.jpg"));
        FutureTask<String> futureTask2 =
                new FutureTask<>(new CopyFile2("D:\\temp\\bb.jpg", "bb.jpg"));
        FutureTask<String> futureTask3 =
                new FutureTask<>(new CopyFile2("D:\\temp\\cc.jpg", "cc.jpg"));
        // 创建线程并启动
        new Thread(futureTask).start();
        new Thread(futureTask2).start();
        new Thread(futureTask3).start();
        // 接收返回值
        try {
            String path1 = futureTask.get();
            String path2 = futureTask2.get();
            String path3 = futureTask3.get();
            System.out.println(path1);
            System.out.println(path2);
            System.out.println(path3);
            // 将新的路径保存到文件
            FileWriter fileWriter = new FileWriter("path.txt");
            fileWriter.write(path1);
            fileWriter.write(System.lineSeparator());
            fileWriter.write(path2);
            fileWriter.write(System.lineSeparator());
            fileWriter.write(path3);
            // flush
            fileWriter.flush();
            // close
            fileWriter.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class CopyFile2 implements Callable<String>{
    File oldFile;
    File newFile;

    public CopyFile2(String oldFile, String newFile) {
        this.oldFile = new File(oldFile);
        this.newFile = new File(newFile);
    }
    @Override
    public String call() throws Exception {
        // 复制文件操作
        try(FileInputStream in = new FileInputStream(oldFile);
            FileOutputStream out = new FileOutputStream(newFile)) {

            // 边读边写
            int readCount;

            byte[] bytes = new byte[1024];
            // 用来做累加的
            double temp = 0;
            long length = oldFile.length();
            while ((readCount = in.read(bytes)) != -1) {
                // write
                out.write(bytes);
                temp += readCount;
                double persent = temp / length * 100;
                System.out.println(oldFile + "复制了:"+String.format("%.2f",persent)+"%");
            }
            System.out.println(oldFile + "复制完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 返回的是新的文件路径
        return newFile.getAbsolutePath();
    }
}

